package mutilthread;

/**
 * Created with IntelliJ IDEA.
 * Description: homework
 * User: Li_yizYa
 * Date: 2025—03—21
 * Time: 17:14
 */
public class Demo4 {
    private static int sum1 = 0;
    private static int sum2 = 0;
    public static void main(String[] args) throws InterruptedException {
        long begin = System.currentTimeMillis();//记录程序开始执行时间
        int[] arr = new int[10000];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = i + 1;
        }

        Thread t1 = new Thread(() -> {
            for (int i = 0; i < arr.length; i += 2) {
                sum1 += arr[i];
            }
        });

        Thread t2 = new Thread(() -> {
            for (int i = 1; i < arr.length; i += 2) {
                sum2 += arr[i];
            }
        });

        t1.start();
        t2.start();
        //main 线程等待 t1 和t2 结束
        t1.join();
        t2.join();

        System.out.println("sum1 = " + sum1);
        System.out.println("sum2 = " + sum2);
        System.out.println("sum = " + sum1 + sum2);

        long end = System.currentTimeMillis();//记录程序结束的时间
        System.out.println("程序运行时间: "  + (end - begin) + "ms");
    }
}
